import 'package:flutter/material.dart';
import "package:get/get.dart";
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../../common/style.dart';
import '../../../components/appbar_widget/appbar_widget.dart';
import '../../../components/loading/svg_loading.dart';
import '../../../components/status/empty.dart';
import '../../../components/status/load_more_state.dart';
import '../../../components/status/not_network.dart';
import '../../../config/global_config.dart';
import '../../../controller/network/network_ctrl.dart';
import '../../../model/bean/todo_bean.dart';
import '../../../utils/screenutil/fit.dart';
import 'ctrl/today_todo_ctrl.dart';

class ToDayTodo extends StatelessWidget {
  static const _WAITING = "今日待维保";
  static const _FUTURE = "未维保";

  // 导入控制器
  final toDaytodoCtrl = Get.put(ToDayTodoCtrl());
  final _nc = Get.find<NetworkCtrl>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarWidget('维保待办').build(context),
      body: Obx(
        () => toDaytodoCtrl.isInitComplete.value
            ? Container(
                height: double.infinity,
                child: Stack(
                  children: [
                    Container(
                      margin:
                          EdgeInsets.only(top: fit(_nc.topDistance.value + 85)),
                      child: toDaytodoCtrl.listData.length > 0
                          ? SmartRefresher(
                              footer: CustomFooter(
                                builder: (BuildContext ct, LoadStatus mode) {
                                  Widget body = LoadMoreState(mode);
                                  return Container(child: Center(child: body));
                                },
                              ),
                              enablePullDown: false,
                              enablePullUp: true,
                              controller: toDaytodoCtrl.refreshController.value,
                              onLoading: toDaytodoCtrl.onLoading,
                              child: ListView.builder(
                                itemCount: toDaytodoCtrl.listData.length,
                                itemBuilder: (BuildContext c, int i) {
                                  // 数据别名
                                  TodoBean alias = toDaytodoCtrl.listData[i];

                                  // 渲染界面
                                  return InkWell(
                                    onTap: () => toDaytodoCtrl
                                        .onLeave(toDaytodoCtrl.listData[i].id),
                                    child: Container(
                                      color: Colors.white,
                                      child: Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.end,
                                        children: [
                                          Container(
                                            padding: EdgeInsets.only(
                                              left: 20.f,
                                              top: 20.f,
                                              bottom: 20.f,
                                            ),
                                            child: Row(
                                              mainAxisAlignment:
                                                  MainAxisAlignment.start,
                                              children: [
                                                Container(
                                                  alignment:
                                                      Alignment.centerLeft,
                                                  width: 420.f,
                                                  child: Column(
                                                    crossAxisAlignment:
                                                        CrossAxisAlignment
                                                            .start,
                                                    mainAxisAlignment:
                                                        MainAxisAlignment
                                                            .center,
                                                    children: [
                                                      Text(
                                                        "${alias.villageName}",
                                                        softWrap: true,
                                                        textAlign:
                                                            TextAlign.left,
                                                        overflow: TextOverflow
                                                            .ellipsis,
                                                        style: TextStyle(
                                                            fontSize: 24.f),
                                                      ),
                                                      SizedBox(height: 10.f),
                                                      Text(
                                                        "${alias.buildingNo}/${alias.unitNo}/${alias.shaft}",
                                                        style: TextStyle(
                                                          fontSize: 22.f,
                                                          color: GlobalConfig
                                                              .titleContent,
                                                        ),
                                                      ),
                                                    ],
                                                  ),
                                                ),
                                                Container(
                                                  width: 140.f,
                                                  alignment: Alignment.center,
                                                  child: Text(
                                                    "${alias.maintenancePlanDate}",
                                                    textAlign: TextAlign.right,
                                                    style: TextStyle(
                                                      fontSize: 22.f,
                                                      color: GlobalConfig
                                                          .titleContent,
                                                    ),
                                                  ),
                                                ),
                                                Container(
                                                  width: 40.f,
                                                  alignment:
                                                      Alignment.centerRight,
                                                  margin: EdgeInsets.only(
                                                    right: 20.f,
                                                  ),
                                                  child: Icon(
                                                    Icons.chevron_right,
                                                    size: 35.f,
                                                    color: GlobalConfig
                                                        .assistColor,
                                                  ),
                                                )
                                              ],
                                            ),
                                          ),
                                          SizedBox(
                                            width: 620.f,
                                            height: 1.f,
                                            child: Container(
                                                color:
                                                    GlobalConfig.borderColor),
                                          ),
                                        ],
                                      ),
                                    ),
                                  );
                                },
                              ),
                            )
                          : Container(
                              width: 640.f,
                              height: double.infinity,
                              child: Center(child: Empty()),
                            ),
                    ),

                    // 选项卡、条件选择器
                    Positioned(
                      child: Container(
                        width: 640.f,
                        height: 85.f,
                        margin: EdgeInsets.only(
                          top: fit(_nc.topDistance.value),
                        ),
                        decoration: BoxDecoration(
                          boxShadow: GlobalConfig.boxShadow,
                        ),
                        child: Column(
                          children: [
                            Container(
                              width: 640.f,
                              height: 85.f,
                              color: Colors.white,
                              child: Flex(
                                direction: Axis.horizontal,
                                children: [
                                  Expanded(
                                    flex: 1,
                                    child: createTabs(
                                      _WAITING,
                                      0,
                                    ),
                                  ),
                                  SizedBox(
                                    width: 1.f,
                                    child: Container(
                                      height: 60.f,
                                      decoration: BoxDecoration(
                                        gradient: Style.gradient,
                                      ),
                                      // color: GlobalConfig.borderColor,
                                    ),
                                  ),
                                  Expanded(
                                    flex: 1,
                                    child: createTabs(
                                      _FUTURE,
                                      2,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),

                    // 网络状态
                    Positioned(top: 0, child: NotNetwork()),
                  ],
                ),
              )
            : SVGLoading(),
      ),
    );
  }

  /// 创建头部选项卡
  /// * [name] String 选项卡名称|必填
  /// * [currentStatus] MaintenanceStatus 当前状态|必填
  Widget createTabs(String name, int currentStatus) {
    return InkWell(
      onTap: () => toDaytodoCtrl.onChangeSelectStatus(currentStatus),
      child: Container(
        alignment: Alignment.center,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              width: 220.f,
              height: 85.f,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                border: Border(
                  bottom: BorderSide(
                    width: 2,
                    color: currentStatus == toDaytodoCtrl.status.value
                        ? GlobalConfig.theme
                        : Colors.white,
                  ),
                ),
              ),
              child: Text(
                "$name",
                style: TextStyle(
                  fontSize: 26.f,
                  color: currentStatus == toDaytodoCtrl.status.value
                      ? GlobalConfig.theme
                      : GlobalConfig.assistColor,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
